<template>
  <!-- 特殊送检状态规则表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="375px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="110px">
      <el-form-item label="特定码(后缀)" prop="shaftSuffix">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.shaftSuffix"
          placeholder="请输入特定码(后缀)"
          clearable
        />

        <span v-else>{{ formData.shaftSuffix }}</span>
      </el-form-item>
      <el-form-item label="特性名称" prop="featureName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.featureName"
          placeholder="请输入特性名称"
          clearable
        />

        <span v-else>{{ formData.featureName }}</span>
      </el-form-item>
      <el-form-item label="描述" prop="remarks">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          type="textarea"
          :rows="3"
          maxlength="255"
          placeholder="请输入描述"
        />

        <span v-else>{{ formData.remarks }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "PCMPCMSPECIALRULEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  shaftSuffix: [
    { required: true, message: "请输入特定码(后缀)", trigger: "change" },
    { max: 10, message: "特定码(后缀)不能超过10个字符", trigger: "blur" },
  ],
  featureName: [
    { required: true, message: "请输入特性名称", trigger: "change" },
    { max: 10, message: "特定码(后缀)不能超过20个字符", trigger: "blur" },
  ],
  remarks: [
    { max: 10, message: "特定码(后缀)不能超过100个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  shaftSuffix: "",
  featureName: "",
  remarks: "",
  version: "",
  enabled: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  lastUpdateTime: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        shaftSuffix,
        featureName,
        remarks,
        version,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      } = newVal;
      Object.assign(formData, {
        id,
        shaftSuffix,
        featureName,
        remarks,
        version,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.shaftSuffix = "";
  formData.featureName = "";
  formData.remarks = "";
  formData.version = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.lastUpdateTime = "";
}

const loading = ref(false);

/** 特殊送检状态规则保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/dmpDosPcm/pcmspecialrule/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
